package com.znxs.bs_c.controller;

import cn.hutool.log.Log;
import cn.hutool.log.level.InfoLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.znxs.bs_c.entity.Menu;
import com.znxs.bs_c.mapper.RoleMapper;
import com.znxs.bs_c.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.znxs.bs_c.common.Result;    // 注意 ，这里要根据包名修改

import com.znxs.bs_c.service.IRoleService;
import com.znxs.bs_c.entity.Role;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 左拿
 * @since 2023-08-21
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IMenuService menuService;


    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Role role){
        return roleService.saveOrUpdate(role)? Result.success() : Result.error(500, "保持失败");
    }

    // 权限角色添加菜单
    @PostMapping("/menu")
    public Result addMenuIdAndRoleId(@RequestBody() Object checkedKeys){
        Log log = Log.get();
        Map<String, Object> values = (Map<String, Object>) checkedKeys;
        Integer id = (Integer) values.get("id");
        List<Integer> keys = (List<Integer>) values.get("keys");
        log.info(keys.toString());
        return  roleService.addMenuIdAndRoleId(id, keys);
    }


    // 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        return roleService.removeById(id)? Result.success() : Result.error(500, "删除失败");
    }

    // 根据id批量删除
    @DeleteMapping
    public Result deleteByIds(@RequestBody List<Integer> Ids){
        return roleService.removeByIds(Ids)? Result.success() : Result.error(500, "删除失败");
    }

    // 根据id查询menuid
    @GetMapping("/roleId/{roleId}")
    public Result getMenuIdByRoleId(@PathVariable("roleId") Integer roleId){
        return roleService.getMenuIdByRoleId(roleId);
    }

    // 查询所有
    @GetMapping
    public Result findAll(){
        return Result.success(roleService.list());
    }

    // 根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id){
        return Result.success(roleService.getById(id));
    }

    // 根据flag查询roleName
    @GetMapping("/flag")
    public Result getFlagName(@RequestParam("flag")String flag){
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.select("name").eq("flag",flag);
        Role one = roleService.getOne(roleQueryWrapper);
        return Result.success(one.getName());
    }

    // 根据flag 查询 menu
    @GetMapping("/flag/{flag}")
    public Result getMenuBYFlag(@PathVariable("flag")String flag){
        Integer roleId = roleMapper.getRoleIdByFlag(flag);
        // 根据roleId查询menuId
        List<Integer> menuIds = roleMapper.getMenuIdByRoleId(roleId);
        // 根据id查询menu
        List<Menu> menus = menuService.getBaseMapper().selectBatchIds(menuIds);
        return  Result.success(menus);
    }


    // 分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam("current") Integer current,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam(value = "name",required = false) String name){
        IPage<Role> page = new Page<>(current, pageSize);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.like("name",name);
        roleQueryWrapper.orderByDesc("id");
        return Result.success(roleService.page(page,roleQueryWrapper));
    }



}

